// ignore_for_file: must_be_immutable

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(title: const Text('这是列表页面')),
      body: const MyText(),
    ),
  ));
}

class MyText1 extends StatelessWidget {
  const MyText1({super.key});

  void Function()? onPress() {
    print('被点了');
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(10),
      child: Wrap(
        // 水平还是垂直排列
        direction: Axis.horizontal,
        // 水平间距
        spacing: 10,
        // 垂直间距
        runSpacing: 10,
        // 元素的对齐方式
        alignment: WrapAlignment.center,
        children: [
          MyButton('这是ifffff按耨i', onPress),
          MyButton('这是i按耨i', onPress),
          MyButton('这是i按耨i', onPress),
          MyButton('这是i按耨i', onPress),
          MyButton('这是i按耨i', onPress),
          MyButton('这是i按耨i', onPress),
          MyButton('这是i按耨i', onPress),
          MyButton('这是i按耨i', onPress),
          MyButton('这是i按耨i', onPress),
          MyButton('这是i按耨i', onPress),
          MyButton('这是i按耨i', onPress),
        ],
      ),
    );
  }
}

class MyText extends StatelessWidget {
  const MyText({super.key});

  void Function()? onPress(k) {
    print(k);
    return null;
  }

  List<Widget> renderButtonList() {
    List<Widget> tempList = [];
    for (var k = 0; k < 10; k++) {
      tempList.add(MyButton('按钮$k', () {
        onPress(k);
      }));
    }
    return tempList;
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Stack(
      children: [
        ListView(
          padding: const EdgeInsets.all(10),
          children: [
            Text('热搜', style: Theme.of(context).textTheme.titleLarge),
            const Divider(),
            Wrap(
              spacing: 10,
              runSpacing: 10,
              children: renderButtonList(),
            ),
            const SizedBox(
              height: 10,
            ),
            Text('历史记录', style: Theme.of(context).textTheme.titleLarge),
            const Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: EdgeInsets.all(10),
                  child: Text('女装'),
                ),
                Padding(
                  padding: EdgeInsets.all(10),
                  child: Text('女装'),
                ),
              ],
            )
          ],
        ),
        Positioned(
            left: 0,
            bottom: 0,
            child: Container(
              width: size.width,
              height: 50,
              padding: const EdgeInsets.all(10),
              child: ElevatedButton.icon(
                onPressed: () {},
                icon: const Icon(Icons.delete),
                label: const Text('清空历史记录'),
                style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(
                        const Color.fromARGB(242, 255, 244, 244)),
                    foregroundColor: MaterialStateProperty.all(Colors.black45)),
              ),
            ))
      ],
    );
  }
}

class MyButton extends StatelessWidget {
  String text;
  void Function()? onPress;
  MyButton(this.text, this.onPress, {super.key});

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
        onPressed: onPress,
        style: ButtonStyle(
            foregroundColor: MaterialStateProperty.all(Colors.black45),
            backgroundColor: MaterialStateProperty.all(
                const Color.fromARGB(242, 255, 244, 244))),
        child: Text(text));
  }
}
